package link.ten_fei.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import link.ten_fei.Bean.Doc_User;
import link.ten_fei.service.AddUserService;
import link.ten_fei.service.AddUserServiceImpl;
import link.ten_fei.util.MD5Util;

/**
 * Servlet implementation class AddUserServlet
 */
@WebServlet("/addUser.action")
public class AddUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 设置前端页面传递回来的字符编码
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		String id = request.getParameter("id");
		// 添加到保存用户界面
		String doc_name = request.getParameter("doc_name");
		String doc_username = request.getParameter("doc_name");
		String doc_sex = request.getParameter("doc_sex");
		String doc_age = request.getParameter("doc_age");
		String doc_title = request.getParameter("doc_title");
		String doc_department = request.getParameter("doc_department");
		String doc_tel = request.getParameter("doc_tel");
		AddUserService addUserService = new AddUserServiceImpl();
		// 查询重复
		if (action == null) {
			// 跳转到添加用户界面
			request.setAttribute("mainRight", "/WEB-INF/jsp/addUser.jsp");
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
			return;
		}
		if (id == null) {
			Doc_User doc_User = addUserService.findByNameOrId(doc_name, doc_age, 0);
			if (action != null && action.equals("ajaxUser") && doc_User != null) {
				response.setCharacterEncoding("utf-8");
				PrintWriter writer = response.getWriter();
				writer.print("当前用户存在！！！");
				return;
			} else if (action != null && action.equals("add") && doc_User == null) {
				// 用户不存在，添加用户
				// 将用户输入的内容封装在一个对象中
				Doc_User user = new Doc_User();

				user.setDoc_name(doc_name);
				user.setDoc_username(doc_username);
				// 初始化密码为123456
				user.setDoc_password(MD5Util.encodeByMD5("123456"));
				user.setDoc_sex(doc_sex);
				user.setDoc_age(doc_age);
				user.setDoc_title(doc_title);
				user.setDoc_department(doc_department);
				user.setDoc_tel(doc_tel);
				user.setDoc_judge(1);
				int flag = addUserService.save(user);
				// 提示信息
				request.setAttribute("success_add", flag);
				// 跳转到添加用户界面
				request.setAttribute("mainRight", "/WEB-INF/jsp/addUser.jsp");
				request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
			}
		} else {
			Doc_User doc_User = addUserService.findByNameOrId(null, null, Integer.parseInt(id));
			String old_name = doc_User.getDoc_name();
			String old_age = doc_User.getDoc_age();
			if (action != null && action.equals("ajaxUser") && doc_User != null
					&& (!old_name.equals(doc_name) && !old_age.equals(doc_age))) {
				response.setCharacterEncoding("utf-8");
				PrintWriter writer = response.getWriter();
				writer.print("当前用户存在！！！");
				return;
			} else if (action != null && action.equals("modify") && doc_User == null
					|| (old_name.equals(doc_name) || old_age.equals(doc_age))) {
				Doc_User user = new Doc_User();
				user.setDoc_name(doc_name);
				user.setDoc_sex(doc_sex);
				user.setDoc_age(doc_age);
				user.setDoc_title(doc_title);
				user.setDoc_department(doc_department);
				user.setDoc_tel(doc_tel);
				user.setDoc_judge(1);
				int value = addUserService.change(user, Integer.parseInt(id));
				request.setAttribute("success_modify", value);
				request.setAttribute("mainRight", "/WEB-INF/jsp/addUser.jsp");
				request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
			}
		}
	}

}
